
import requestInstance from '@/utils/request'
/**
 * 获取用户列表
 * @param {Object} params - 查询参数
 * @returns {Promise} 用户列表
 */
export const getUsers = async (params = {}) => {
  try {
    const response = await requestInstance.get('users', {
      params
    });
    return response.data;
  } catch (error) {
    console.error('获取用户列表失败:', error);
    throw error;
  }
};

/**
 * 获取当前登录用户信息
 * @returns {Promise} 当前用户信息
 */
export const getCurrentUser = async () => {
  try {
    const response = await requestInstance.get('users/current');
    return response.data;
  } catch (error) {
    console.error('获取当前用户信息失败:', error);
    throw error;
  }
};

/**
 * 创建用户
 * @param {Object} userData - 用户数据
 * @returns {Promise} 创建结果
 */
export const createUser = async (userData) => {
  try {
    const response = await requestInstance.post('users', userData);
    return response.data;
  } catch (error) {
    console.error('创建用户失败:', error);
    throw error;
  }
};

/**
 * 更新用户
 * @param {string|number} id - 用户ID
 * @param {Object} userData - 用户数据
 * @returns {Promise} 更新结果
 */
export const updateUser = async (id, userData) => {
  try {
    const response = await requestInstance.put(`/api/users/${id}`, userData);
    return response.data;
  } catch (error) {
    console.error('更新用户失败:', error);
    throw error;
  }
};

/**
 * 删除用户
 * @param {string|number} id - 用户ID
 * @returns {Promise} 删除结果
 */
export const deleteUser = async (id) => {
  try {
    const response = await requestInstance.delete(`/api/users/${id}`);
    return response.data;
  } catch (error) {
    console.error('删除用户失败:', error);
    throw error;
  }
};

/**
 * 批量删除用户
 * @param {Array} ids - 用户ID数组
 * @returns {Promise} 删除结果
 */
export const batchDeleteUsers = async (ids) => {
  try {
    const response = await requestInstance.post('users/batch-delete', {
      ids
    });
    return response.data;
  } catch (error) {
    console.error('批量删除用户失败:', error);
    throw error;
  }
};

/**
 * 切换用户状态（启用/禁用）
 * @param {string|number} id - 用户ID
 * @param {Object} statusData - 状态数据
 * @returns {Promise} 更新结果
 */
export const toggleUserStatus = async (id, statusData) => {
  try {
    const response = await requestInstance.put(`/api/users/${id}/status`, statusData);
    return response.data;
  } catch (error) {
    console.error('切换用户状态失败:', error);
    throw error;
  }
};

/**
 * 更新用户角色
 * @param {string|number} userId - 用户ID
 * @param {Object} roleData - 角色数据
 * @returns {Promise} 更新结果
 */
export const updateUserRole = async (userId, roleData) => {
  try {
    const response = await requestInstance.put(`/api/users/${userId}/role`, roleData);
    return response.data;
  } catch (error) {
    console.error('更新用户角色失败:', error);
    throw error;
  }
};

/**
 * 重置用户密码
 * @param {string|number} userId - 用户ID
 * @param {Object} passwordData - 密码数据
 * @returns {Promise} 重置结果
 */
export const resetUserPassword = async (userId, passwordData) => {
  try {
    const response = await requestInstance.put(`/api/users/${userId}/reset-password`, passwordData);
    return response.data;
  } catch (error) {
    console.error('重置用户密码失败:', error);
    throw error;
  }
};